import java.awt.Color;
import java.awt.Component;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class mainMap {

	/**
	 * @author Matthew Rowe
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {

		Draw D = new Draw();

		Component frame = null;


		/*
		 * Create our Data Vector. The Vector holds Vectors of all the data points
		 * returned in an output file. This makes it easier to store all the values
		 * and still be able to sort through easily
		 */
		List<Vector<Double>> Data = new LinkedList<Vector<Double>>();
		Data = new ArrayList<Vector<Double>>();


		/*
		 * Create a file chooser which allows the user to point to where the 
		 * 
		 * file is located. Then use the given file to read through the data. 
		 * While reading store all the values in our Data Vector
		 */
		final JFileChooser fc = new JFileChooser();
		fc.showOpenDialog(fc);

		File dataFile = fc.getSelectedFile();
		ReadWithScanner readData = null;
		try {
			readData = new ReadWithScanner(dataFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			Data = readData.processLineByLine();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}


		//Initialize our data values from our Data vector
		Vector<Double> X = Data.get(0);
		Vector<Double> Y = Data.get(1);
		Vector<Double> Right = Data.get(3);
		Vector<Double> Yaw = Data.get(2);
		Vector<Double> Door = Data.get(4);

		/*
		 * Search through the vectors to find our Max and Min, These are used
		 * to set the limits on the Map.
		 */
		double xMax =  X.get(0).doubleValue();
		double xMin =  X.get(0).doubleValue();
		double yMax =  Y.get(0).doubleValue();
		double yMin =  Y.get(0).doubleValue();	

		for (int j= 1;j <X.size();j++){
			if (xMax < X.get(j).doubleValue() ){
				xMax =  X.get(j).doubleValue();
			}
			if(xMin > X.get(j).doubleValue()){
				xMin =  X.get(j).doubleValue();
			}
			if(yMax < Y.get(j).doubleValue()){
				yMax = Y.get(j).doubleValue();
			}
			if(yMin > Y.get(j).doubleValue()){
				yMin = Y.get(j).doubleValue();
			}
		}
		D.drawString("XScale: " + (Math.round((xMax) - (xMin))) + " Meters  YScale = " +(Math.round((yMax) - (yMin))) + " Meters" );

		double limitMax = Right.get(0).doubleValue()/1000;
		for (int r = 1; r < Right.size();r++){
			if (Right.get(r).doubleValue()/1000 > limitMax)
				limitMax = Right.get(r).doubleValue()/1000;
		}

		if (xMax > yMax)
			yMax = xMax;
		if (yMax > xMax)
			xMax = yMax;
		if (xMin < yMin)
			yMin = xMin;
		if (yMin < xMin)
			xMin = yMin;

		if (limitMax > xMax)
			xMax = limitMax;
		if (limitMax > yMax)
			yMax = limitMax;

		/*
		 * Initialize our Drawing frame and set some of the initial parameters. 
		 */


		D.setXscale(xMin - 5, xMax + 5);
		D.setYscale(yMin - 5, yMax + 5);
		
		//D.setPenColor(new Color(0,149,0));
		D.setPenColor(Color.red);
		D.setPenRadius(.002);

		/*
		 * Loop through our Data Values and plot Draw the lines, Currently it only
		 * draws the X and Y coordinates.
		 */
		
		for (int i = 0;i < X.size()-1; i++){
			D.line(((Double) X.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue(), ((Double) Y.get(i+1)).doubleValue());
		}

		for (int i = 0;i < Right.size()-1; i++){
			Right.set(i, Right.get(i)/1000);
		}


		int yawCheck = -1;
		int index = 0;
		double count = 1.0;
		double total = 0.0;
		double rightAvg = Right.get(0).doubleValue();
		int counter = 0;
		double rightCheck = 1;

		for (int i = 0;i < Door.size()-1; i++){
			


			//Average based on right spikes
			System.out.println("RIGHT: " +Right.get(i).doubleValue() );
			System.out.println("-RightAvf: " +Math.abs(Right.get(i).doubleValue() - rightAvg));
			if (Math.abs(Right.get(i).doubleValue() - rightAvg) > rightCheck){
				rightAvg = Right.get(i).doubleValue();
				System.out.println(rightAvg);
			}



			if(Door.get(i).intValue()==1){
				D.setPenColor(new Color(0,156,250));
				//D.setPenColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
				D.setPenRadius(.005);
			}else{
				D.setPenColor(Color.BLACK);
				D.setPenRadius(.002);
			}

			
			/*switch(Yaw.get(i).intValue()){

			case 0:
				D.line(((Double) X.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue() -  rightAvg,((Double) X.get(i+1)).doubleValue(), ((Double) Y.get(i+1)).doubleValue() - rightAvg);
				//System.out.println("0 case");
				break;

			case 90:
				D.line(((Double) X.get(i)).doubleValue() + rightAvg, ((Double) Y.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue() + rightAvg, ((Double) Y.get(i+1)).doubleValue());
				//System.out.println("90 case");
				break;

			case 180:
				D.line(((Double) X.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue()  + rightAvg,((Double) X.get(i+1)).doubleValue(), ((Double) Y.get(i+1)).doubleValue() + rightAvg);
				//System.out.println("180 case");
				break;

			case 270:
				D.line(((Double) X.get(i)).doubleValue() - rightAvg, ((Double) Y.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue() - rightAvg, ((Double) Y.get(i+1)).doubleValue());
				//System.out.println("270 case");
				break;


			default: System.out.println("FUCK THE POLICE"); 

			}*/
			
			switch(Yaw.get(i).intValue()){

			case 0:
				D.line(((Double) X.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue() -  ((Double) Right.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue(), ((Double) Y.get(i+1)).doubleValue() - ((Double) Right.get(i)).doubleValue());
				//System.out.println("0 case");
				break;

			case 90:
				D.line(((Double) X.get(i)).doubleValue() + ((Double) Right.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue() + ((Double) Right.get(i)).doubleValue(), ((Double) Y.get(i+1)).doubleValue());
				//System.out.println("90 case");
				break;

			case 180:
				D.line(((Double) X.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue()  + ((Double) Right.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue(), ((Double) Y.get(i+1)).doubleValue() + ((Double) Right.get(i)).doubleValue());
				//System.out.println("180 case");
				break;

			case 270:
				D.line(((Double) X.get(i)).doubleValue() - ((Double) Right.get(i)).doubleValue(), ((Double) Y.get(i)).doubleValue(),((Double) X.get(i+1)).doubleValue() - ((Double) Right.get(i)).doubleValue(), ((Double) Y.get(i+1)).doubleValue());
				//System.out.println("270 case");
				break;


			default: System.out.println("FUCK THE POLICE"); 
	}


			//System.out.println("X = "+((Double) X.get(i)).doubleValue()+ "  Y= "+((Double) Y.get(i)).doubleValue());
			counter--;
		}
		/*
		 * Alert the user when the drawing is complete
		 */

		JOptionPane.showMessageDialog(frame,
				"The map drawing process is complete!", 
				"Complete", 1);

	}


}

